﻿using System;

namespace UniRx.Operators
{
	// Token: 0x02000309 RID: 777
	internal class StartObservable<T> : OperatorObservableBase<T>
	{
		// Token: 0x06001058 RID: 4184 RVA: 0x0004B728 File Offset: 0x00049B28
		public StartObservable(Func<T> function, TimeSpan? startAfter, IScheduler scheduler) : base(scheduler == Scheduler.CurrentThread)
		{
			this.function = function;
			this.startAfter = startAfter;
			this.scheduler = scheduler;
		}

		// Token: 0x06001059 RID: 4185 RVA: 0x0004B74D File Offset: 0x00049B4D
		public StartObservable(Action action, TimeSpan? startAfter, IScheduler scheduler) : base(scheduler == Scheduler.CurrentThread)
		{
			this.action = action;
			this.startAfter = startAfter;
			this.scheduler = scheduler;
		}

		// Token: 0x0600105A RID: 4186 RVA: 0x0004B774 File Offset: 0x00049B74
		protected override IDisposable SubscribeCore(IObserver<T> observer, IDisposable cancel)
		{
			if (this.startAfter != null)
			{
				return this.scheduler.Schedule(this.startAfter.Value, new Action(new StartObservable<T>.StartObserver(this, observer, cancel).Run));
			}
			return this.scheduler.Schedule(new Action(new StartObservable<T>.StartObserver(this, observer, cancel).Run));
		}

		// Token: 0x04000997 RID: 2455
		private readonly Action action;

		// Token: 0x04000998 RID: 2456
		private readonly Func<T> function;

		// Token: 0x04000999 RID: 2457
		private readonly IScheduler scheduler;

		// Token: 0x0400099A RID: 2458
		private readonly TimeSpan? startAfter;

		// Token: 0x0200030A RID: 778
		private class StartObserver : OperatorObserverBase<T, T>
		{
			// Token: 0x0600105B RID: 4187 RVA: 0x0004B7DF File Offset: 0x00049BDF
			public StartObserver(StartObservable<T> parent, IObserver<T> observer, IDisposable cancel) : base(observer, cancel)
			{
				this.parent = parent;
			}

			// Token: 0x0600105C RID: 4188 RVA: 0x0004B7F0 File Offset: 0x00049BF0
			public void Run()
			{
				T value = default(T);
				try
				{
					if (this.parent.function != null)
					{
						value = this.parent.function();
					}
					else
					{
						this.parent.action();
					}
				}
				catch (Exception error)
				{
					try
					{
						this.observer.OnError(error);
					}
					finally
					{
						base.Dispose();
					}
					return;
				}
				this.OnNext(value);
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x0600105D RID: 4189 RVA: 0x0004B8AC File Offset: 0x00049CAC
			public override void OnNext(T value)
			{
				try
				{
					this.observer.OnNext(value);
				}
				catch
				{
					base.Dispose();
					throw;
				}
			}

			// Token: 0x0600105E RID: 4190 RVA: 0x0004B8E8 File Offset: 0x00049CE8
			public override void OnError(Exception error)
			{
				try
				{
					this.observer.OnError(error);
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x0600105F RID: 4191 RVA: 0x0004B920 File Offset: 0x00049D20
			public override void OnCompleted()
			{
				try
				{
					this.observer.OnCompleted();
				}
				finally
				{
					base.Dispose();
				}
			}

			// Token: 0x0400099B RID: 2459
			private readonly StartObservable<T> parent;
		}
	}
}
